package com.xuecheng.auth.controller;

import com.xuecheng.auth.service.WxAuthService;
import com.xuecheng.ucenter.model.po.XcUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.Objects;

/**
 * @author Tang
 * @version 1.0
 * @date 2024/1/11 21:04
 */
@Slf4j
@Controller
public class WxController {
    @Autowired
    WxAuthService wxAuthService;
    private final static String REDIRECT_HTML_URL = "redirect:http://www.51xuecheng.cn/sign.html?username=%s&authType=wx";
    private final static String REDIRECT_HTML_ERROR_URL = "redirect:http://www.51xuecheng.cn/error.html";

    @RequestMapping("/wxLogin")
    public String wxLogin(String code, String state) throws IOException {
        log.debug("微信扫码登录回调, code:{}, state:{}", code, state);
        XcUser xcUser = wxAuthService.wxAuth(code);
        if (Objects.isNull(xcUser)) {
            return REDIRECT_HTML_ERROR_URL;
        }
        return String.format(REDIRECT_HTML_URL, xcUser.getId());
    }
}
